/**
 * 
 */
package org.t2.game.server.module.player.service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.gaming.service.repository.Repository;
import org.gaming.tool.ConsoleUtil;
import org.gaming.tool.DateTimeUtil;
import org.gaming.tool.LocalDateTimeUtil;
import org.gaming.tool.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.t2.game.server.module.player.model.Player;

import jakarta.annotation.PostConstruct;
import sh.t2.share.module.player.log.PlayerCreateLog;
import sh.t2.share.module.player.log.PlayerLoginLog;
import sh.t2.share.module.player.log.PlayerRechargeLog;

/**
 * @author YY
 */
@Controller
public class BuildTestLogHandler {

	@Autowired
	private PlayerLogger playerLogger;
	
	public void build() {
		long idGen = 0;
		long now = DateTimeUtil.currMillis();
		
		List<Player> players = new ArrayList<>();
		for(int i = 0; i < 7; i++) {
			long createTime = now + DateTimeUtil.ONE_DAY_MILLIS * i;
			int createNum = RandomUtil.closeOpen(50, 100);
			for(int j = 0; j < createNum; j++) {
				idGen += 1;
				Player player = new Player();
				player.setId(idGen);
				player.setInsertTime(LocalDateTimeUtil.ofEpochMilli(createTime));
				player.setOpenId(UUID.randomUUID().toString());
				player.setNick(player.getOpenId().substring(0, 8));
				player.setCreateIp("127.0.0.1");
				player.setLoginIp("127.0.0.1");
				player.setLastLoginTime(createTime);
				player.setChannelId(2);
				int newRate = RandomUtil.closeClose(1, 10000);
				if(newRate <= 7000) {
					player.setNewComer(true);
				} else {
					player.setNewComer(false);
				}
				players.add(player);
			}
		}
		
		List<PlayerCreateLog> createLogs = new ArrayList<>();
		List<PlayerLoginLog> loginLogs = new ArrayList<>();
		for(Player player : players) {
			PlayerCreateLog createLog = playerLogger.formCreateLog(player);
			createLogs.add(createLog);
			
			PlayerLoginLog loginLog = playerLogger.formLoginLog(player);
			loginLogs.add(loginLog);
		}
		Repository.get(PlayerCreateLog.class).insertAll(createLogs);
		Repository.get(PlayerLoginLog.class).insertAll(loginLogs);
		
		loginLogs.clear();
		List<PlayerRechargeLog> rechargeLogs = new ArrayList<>();
		for(Player player : players) {
			int rate = RandomUtil.closeClose(1, 10000);
			long money = RandomUtil.closeClose(12, 648);
			if(rate <= 3000) {
				//30%的玩家创号当天充值
				player.setCreatePay(true);
				
				PlayerRechargeLog rechargeLog = playerLogger.formRechargeLog(player, LocalDateTimeUtil.toEpochMilli(player.getInsertTime()), money);
				rechargeLogs.add(rechargeLog);
				
				PlayerLoginLog loginLog = playerLogger.formLoginLog(player);
				loginLogs.add(loginLog);
			} else if(rate <= 5000) {
				//20%的玩家创号后两天才充值
				player.setFirstPayTime(LocalDateTimeUtil.toEpochMilli(player.getInsertTime()) + DateTimeUtil.ONE_DAY_MILLIS * 2);
			
				PlayerRechargeLog rechargeLog = playerLogger.formRechargeLog(player, player.getFirstPayTime(), money);
				rechargeLogs.add(rechargeLog);
				
				PlayerLoginLog loginLog = playerLogger.formLoginLog(player);
				loginLogs.add(loginLog);
			}
		}
		Repository.get(PlayerLoginLog.class).insertAll(loginLogs);
		Repository.get(PlayerRechargeLog.class).insertAll(rechargeLogs);
		
		
		loginLogs.clear();
		for(int i = 1; i <= 30; i++) {
			for(Player player : players) {
				int random = RandomUtil.closeClose(1, 10000);
				if(random <= 5000) {
					long createTime = LocalDateTimeUtil.toEpochMilli(player.getInsertTime());
					player.setLastLoginTime(createTime + DateTimeUtil.ONE_DAY_MILLIS * i);
					PlayerLoginLog loginLog = playerLogger.formLoginLog(player);
					loginLogs.add(loginLog);
				}
			}
		}
		System.out.println("loginLogs " + loginLogs.size());
		Repository.get(PlayerLoginLog.class).insertAll(loginLogs);
		
		System.out.println("添加测试日志完成");
	}
	
	@PostConstruct
	private void console() {
		ConsoleUtil.addFunction("log", () -> {
			build();
		});
	}
}
